package com.example.myactspringboot;

import com.example.myactspringboot.annotation.CHMapper;
import com.example.myactspringboot.annotation.DBMapper;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import com.github.pagehelper.page.PageAutoDialect;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;
import java.util.Properties;

//@Configuration
//不要@EnableDatabaseAutoConfig
public class SpringContextMybatis  implements EnvironmentAware  {

    private Environment env;

	@Bean(name="sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(DataSource dsMysql,ApplicationContext applicationContext) throws Exception{
		//USE logback
		org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dsMysql);
        sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/**/*.xml"));
        sessionFactory.setConfigLocation(new ClassPathResource("config/mybatis-config.xml"));
        return sessionFactory.getObject();
	}


	@Bean
	@DependsOn("sqlSessionFactory")
    @Primary
	public MapperScannerConfigurer dbMapperScannerConfigurer(){
		MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
		Properties properties = new Properties();
		properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
		mapperScannerConfigurer.setProperties(properties);
		mapperScannerConfigurer.setBasePackage(env.getProperty("mybatis.MapperScan"));
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
		mapperScannerConfigurer.setAnnotationClass(DBMapper.class);
		return mapperScannerConfigurer;
	}

/*	@Bean
	public MessagePageAnnoAop messagePageAop(){
		MessagePageAnnoAop messagePageAop=new MessagePageAnnoAop();
		messagePageAop.addPackageNames("com.grkj");
		return messagePageAop;
	}*/


	//  ================================clickhouse
	@Bean(name = "sqlSessionFactory-clickhouse")
	public SqlSessionFactory sqlSessionFactoryClickhouse(@Qualifier("datasource-clickhouse")DataSource dsMain, ApplicationContext applicationContext)
			throws Exception {
		// USE LOG4J
		org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dsMain);
		sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/**/*.xml"));
		sessionFactory.setConfigLocation(new ClassPathResource("config/mybatis-config.xml"));
		//配置pageHelper方言
//		PageAutoDialect.registerDialectAlias("clickhouse", MySqlDialect.class);

		return sessionFactory.getObject();
	}

	@Bean
	@DependsOn("sqlSessionFactory-clickhouse")
	public MapperScannerConfigurer dbMapperScannerConfigurerClickhouse() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		Properties properties = new Properties();
		properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
		mapperScannerConfigurer.setProperties(properties);
		mapperScannerConfigurer.setBasePackage(env.getProperty("mybatis.MapperScan"));
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory-clickhouse");
		mapperScannerConfigurer.setAnnotationClass(CHMapper.class);
		return mapperScannerConfigurer;
	}
	// ================================clickhouse====

	@Override
	public void setEnvironment(Environment environment) {
		// TODO Auto-generated method stub
		this.env=environment;
	}

}
